//
//  CategoryViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/10.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryCollectionViewCell.h"
#import "CategoryModel.h"

@interface CategoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionViewFlowLayout *cateFlowLayout;

@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    [self initCateCollectionView];
    [self getDataWithUrl:@"http://mobile.ximalaya.com/m/category_list?device=iPhone&type=user"];
}

/**
 *  初始化最外层的collectionView
 */
- (void)initCateCollectionView
{
    self.cateFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.cateFlowLayout.itemSize = CGSizeMake(FM_Screen_Width/4,30);
    self.cateFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.cateFlowLayout.minimumLineSpacing = 0;
    self.cateFlowLayout.minimumInteritemSpacing = 0;
    self.cateFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.cateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, FM_Screen_Width, FM_Screen_Height/3) collectionViewLayout:self.cateFlowLayout];
    self.cateCollectionView.dataSource = self;
    self.cateCollectionView.delegate = self;
    self.cateCollectionView.showsVerticalScrollIndicator = NO;
    self.cateCollectionView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    [self.view addSubview:_cateCollectionView];
    
    // 分类
    [self.cateCollectionView registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:@"cateItem"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionViewCell *cateCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cateItem" forIndexPath:indexPath];
    cateCell.layer.borderColor=[UIColor lightGrayColor].CGColor;
    cateCell.layer.borderWidth=0.3;
    CategoryModel *cateModel = [self.dataArray objectAtIndex:indexPath.row];
    cateCell.categoryModel = cateModel;
    
    // 默认选中
//    if ([cateModel.title isEqualToString:self.selectedTitleStr]) {
//        cateCell.titleLabel.textColor = [UIColor orangeColor];
//        cateCell.titleLabel.backgroundColor = UIColorFromRGB(0xe7e7e7);
//    }
    return cateCell;
}

// 选中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionViewCell *cateCell = (CategoryCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cateCell.titleLabel.textColor = [UIColor orangeColor];
    cateCell.titleLabel.backgroundColor = UIColorFromRGB(0xe7e7e7);
//
    // 协议跳转
    CategoryModel *cateModel = [self.dataArray objectAtIndex:indexPath.row];
    [self.delegate anncouncerController:cateModel.name title:cateModel.title];
}

// 取消选中
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionViewCell *cateCell = (CategoryCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cateCell.titleLabel.textColor = [UIColor blackColor];
    cateCell.titleLabel.backgroundColor = [UIColor whiteColor];
}

/**
 *  网络请求
 */
- (void)getDataWithUrl:(NSString *)url
{
    self.dataArray = [NSMutableArray array];
    [NetWorkManager GET:url isCaChe:YES resultData:^(id responseObject) {
        if (responseObject != nil) {
            self.dataArray = [CategoryModel modelWithArr:[responseObject objectForKey:@"list"]];
            [self.cateCollectionView reloadData];
        }
    } resultRrror:^(NSError *error) {
        NSLog(@"分类:error,%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
